<%@page import="java.util.ArrayList"%>
<%@page import="studio7Omega.modelo.Local"%>
<%@page import="studio7Omega.dao.ItemDAO"%>
<%@page import="studio7Omega.dao.SalaDAO"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Documento sin t&iacute;tulo</title>
        <style type="text/css">
            <!--
            .Estilo1 {
                font-size: 24px;
                color: #0000FF;
            }
            -->
        </style>
        <script>
            function getDataServer(url){
                var xml = null;
                try{
                    xml = new ActiveXObject("Microsoft.XMLHTTP");
                }catch(expeption){
                    xml = new XMLHttpRequest();
                }
                xml.open("GET",url, false);
                xml.send(null);
                if(xml.status == 404) alert("Url no valida");
                return xml.responseText;
            }

            function getSalas(id){
                var result = getDataServer("listarSala?idlocal="+id);
                if(result=="error"){
                    alert("Error : No se pudo cargar las Salas");
                }
                else{
                    document.getElementById("divSala").innerHTML = result;
                }
            }
            function getSalaCosto(id){
                var result = getDataServer("obtenerSalaCosto?idsala="+id);
                if(result=="error"){
                    alert("Error : No se pudo cargar la Sala");
                }
                else{                   
                    document.getElementById("txtCostoSala").value=result;
                }
            }

            function getItems(id){
                var result = getDataServer("obtenerSala?idsala="+id);
                if(result=="error"){
                    alert("Error : No se pudo cargar los Items");
                }
                else{
                    document.getElementById("divItem").innerHTML = result;
                }
            }

            function agregarItem(){
                idsala = document.frmArmado.cbSala.options[document.frmArmado.cbSala.selectedIndex].value;
                iditem = document.frmArmado.cbItems.options[document.frmArmado.cbItems.selectedIndex].value;
                if(idsala==0 || iditem==0){
                    alert("Debe Seleccionar una Sala o Item");
                }else{                    
                    var result = getDataServer("agregarItem?idsala="+idsala+"&iditem="+iditem);
                    alert(result);
                    document.frmArmado.cbTipoItem.selectedIndex=1;
                    getListadoItems("instrumento");
                    getItems(idsala);
                    getSalaCosto(idsala);
                }
            }

            function eliminarItem(sala, item){
                var result = getDataServer("eliminarItem?idsala="+sala+"&iditem="+item);
                alert(result);
                document.frmArmado.cbTipoItem.selectedIndex=1;
                getListadoItems("instrumento");
                getItems(sala);
                getSalaCosto(idsala);
            }

            function getListadoItems(tipo){
                var result = "error";
                if(tipo=="instrumento"){
                    var result = getDataServer("listarInstrumentos");
                }
                if(tipo=="servicio"){
                    idsala = document.frmArmado.cbSala.options[document.frmArmado.cbSala.selectedIndex].value;
                    var result = getDataServer("listarServicios?idsala="+idsala);
                }
                if(result=="error"){
                    if(tipo==0){
                        document.getElementById("divItems").innerHTML = "<select name='cbItem' id='cbItem'><option selected='selected' value='0'>Seleccionar</option></select>";
                    }else{
                        alert("Error : No se pudo Listar los Items ");
                    }
                }
                else{
                    document.getElementById("divItems").innerHTML = result;
                }
            }
        </script>
    </head>
    <body bgcolor="#CCCCCC" >
        <form id="frmArmado" name="frmArmado" method="post" action="">
            <p class="Estilo1">Armado de Sala</p>
            <table width="567" border="1">
                <tr>
                    <td width="78">Local</td>
                    <td width="126" colspan="3">
                        <select name="cbLocal" id="cbLocal" onchange="getSalas(this.value)">
                            <option selected="selected" value="0">Seleccione</option>
                            <%
                                        
                                        ArrayList<Local> arr = (ArrayList) session.getAttribute("colLocal");
                                        for (Local local : arr) {
                                            out.println("<option value='" + local.getIdLocal() + "'>" + local.getDescripcion() + "</option>");
                                        }
                            %>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td width="78">Sala</td>
                    <td width="126">
                        <div id="divSala">
                            <select name="cbSala" id="cbSala">
                                <option selected="selected" value="0">Seleccione</option>
                            </select>
                        </div>
                    </td>
                    <td width="100">Costo Sala</td>
                    <td width="144">
                        <label>
                            <input type="text" name="txtCostoSala" id="txtCostoSala" readonly="true"/>
                        </label></td>
                    <td width="114">&nbsp;</td>
                </tr>
                <tr>
                    <td>Tipo Item</td>
                    <td>
                        <select name="cbTipoItem" onchange="getListadoItems(this.value)">
                            <option selected="selected" value="0">Seleccione</option>
                            <option value="instrumento">Instrumento</option>
                            <option value="servicio">Servicio</option>
                        </select>
                    </td>
                    <td colspan="2">
                        <div id="divItems">
                            <select name="cbItems" id="cbItems">
                                <option selected="selected" value="0">Seleccione</option>
                            </select>
                        </div>
                    </td>
                    <td>
                        <label>
                            <input type="button" name="btnRegistrarSala" id="btnRegistrarSala" value="Agregar" onclick="agregarItem()"/>
                        </label>
                    </td>
                </tr>
            </table>
            <p class="Estilo1">
                <label></label>
            </p>
            <div id="divItem">
                <table width="516" border="1">
                    <tr>
                        <td width="96"><strong>Sala</strong></td>
                        <td width="219"><strong>Instrumento</strong></td>
                        <td width="179">&nbsp;</td>
                    </tr>
                </table>
            </div>
            <p class="Estilo1">&nbsp;</p>
        </form>
    </body>
</html>